<template>
	<div id="updateUserDetails" class="updateUserDetails">
		<group label-width="4.5em" label-margin-right="2em" label-align="right">
			<x-input title="昵称" v-model="nickname"></x-input>
		</group>
		<flexbox>
			<flexbox-item>
				<x-button class="flexBtn_confirm" @click.native="updateUser">确定</x-button>
			</flexbox-item>
			<flexbox-item>
				<x-button class="flexBtn_cancel" @click.native="cancelUpdate">取消</x-button>
			</flexbox-item>
		</flexbox>
	</div>
</template>

<script>
	import bus from '@/event/eventBus'
	import NetWorking from '../../../utils/networking'
	import {Group, XInput, XButton, Flexbox, FlexboxItem} from 'vux'

	export default {
		name: "new-user",
		components: {
			Group,
			XInput,
			XButton,
			Flexbox,
			FlexboxItem
		},
		data() {
			return {
				id: this.$store.state.user.userModel.sysUser.id,
				nickname: '',
				password: ''
			}
		},
		created: function () {
			bus.$emit("barController", 1)
			bus.$emit("setTopSimpleTitle", '个人信息修改')

			let _this = this
			NetWorking.doGet(
				'users/'+this.id,
				null,
				null
			).then(
				response => {
					let rs = response.data
					if(rs.error_code == 0){
						_this.nickname = rs.user.nickname
						_this.password = rs.user.password
					}
				}
			)
		},
		methods: {
			cancelUpdate: function () {
				this.$router.go(-1)
			},
			updateUser: function () {
				NetWorking.doPatch(
					'users',
					null,
					{
						id: this.id,
						nickname: this.nickname,
						password: this.password
					},
					null
				).then(
					response => {
						let rs = response.data
						if(rs.error_code == 0){
							this.$router.push({ name: 'selfManage' })
						}else{
							alert(rs.error_message)
						}
					}
				)
			}
		}
	}
</script>

<style>
	.updateUserDetails {
		border: solid;
		border-color: #f7f7fa;
		margin-left: 5px;
		margin-right: 5px;
		margin-top: 56px;
		box-shadow: 5px 5px 5px #888888;
		border-radius: 25px;
		margin-bottom: 100px;
		background-color: white;
	}

	.flexBtn {
		margin-top: 20px;
		margin-bottom: 10px;
		width: 90% !important;
		border-radius: 25px;
	}
</style>